<template>
    <el-main v-loading="loading">
        <el-row :gutter="20" class="margin_bottom_30">
            <el-col :span="4">
                <el-input placeholder="输入话题名称" size="small" v-model="filterData.topicName"></el-input>
            </el-col>
            <el-col :span="4">
                <el-select
                    v-model="filterData.topicType"
                    prop="campaignState"
                    placeholder="请选择话题类型"
                    size="small"
                    style="width:100%"
                    @change="handleTypeChange">
                    <el-option
                        v-for="item in topicTypeList"
                        :key="item.labelId"
                        :label="item.labelText"
                        :value="item.labelId">
                    </el-option>
                </el-select>
            </el-col>
            <el-col :span="4" :offset="1">
                <p style="display: flex">
                    <el-button type="primary" size="small" @click="search">搜 索</el-button>
                    <el-button size="small" @click="resetQuery">重 置</el-button>
                    <el-button type="primary" size="small" @click="handleActionPage">新增话题</el-button>
                </p>
            </el-col>
        </el-row>
        <el-table
            :data="dataList"
            border
            :row-key="row => row.topicUid"
            height="640"
            :header-cell-style="{background:'#afafaf',color:'#606266'}"
            ref="multipleTable"
            @selection-change="handleSelectionChange">
            <el-table-column type="selection" align="center" reserve-selection width="40"></el-table-column>
            <el-table-column prop="topicName" align="center" width="150" label="话题名称"></el-table-column>
            <el-table-column align="center" min-width="300" label="描述">
                <template slot-scope="scope">
                    <el-tooltip :content="scope.row.medalDesc">
                        <p>{{scope.row.medalDesc}}</p>
                    </el-tooltip>
                </template>
            </el-table-column>
            <el-table-column align="center" width="100" label="话题类型">
                 <template slot-scope="scope">
                    <p>{{getType(scope.row.topicType)}}</p>
                </template>
            </el-table-column>
            <el-table-column align="center" width="100" label="话题图">
                <template slot-scope="scope">
                    <el-tooltip content="话题图">
                        <img v-image-preview :src="scope.row.topicImageUrl" width="60" />
                     </el-tooltip>
			    </template>
            </el-table-column>
            <el-table-column prop="createTime" align="center" width="160" label="创建时间"></el-table-column>
            <el-table-column prop="status" align="center" width="100" label="状态">
                <template slot-scope="scope">
                    <span>{{!scope.row.medalStatus ? '已启用' : '未启用'}}</span>
                </template>
            </el-table-column>
            <el-table-column fixed="right" align="center" label="操作" width="200">
                <template slot-scope="scope">
                    <el-button type="text" @click="handleEdit(scope.row)" size="small">编辑</el-button>
                    <el-button type="text" @click="handleDel(scope.row)" size="small">删除</el-button>
                    <el-button type="text" @click="handleUpdate(scope.row)" size="small">
                        {{!scope.row.medalStatus ? '停用' : '启用'}}
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <div class="footer flex-between">
             <el-checkbox v-model="checkAll" :disabled="!dataList.length" @change="handleCheckAllChange">
                全选
            </el-checkbox>
            <el-pagination
                @current-change="handleCurrentChange"
                :current-page="page"
                :page-size="10"
                background
                :disabled="!dataList.length"
                :total="count"
                layout="total, prev, pager, next, jumper">
            </el-pagination>
        </div>
      
		<el-dialog
            :title="!topicInfo.productUid ? '新增话题' : '编辑话题'"
            @close="handleCloseAction"
            :visible.sync="showActionPage"
            destroy-on-close
            :modal-append-to-body="false"
            :close-on-click-modal="false"
            width="40%">
			<topic-action
                ref="topicRef"
                :infoData="topicInfo"
                @func="closeTemplatePage"
                :topicTypeList="topicTypeList">
            </topic-action>
		</el-dialog>
    </el-main>
</template>
<script>
    import topicAction from './topic/index.vue';
    import utils from '@/utils/utils';
    import {
        medalList,
        rightsList,
        deleteMedal,
        updateMedalState
	} from "@/api/table";

    export default {
        name: "TOPIC_LIST",
        filters: {
            controlLength: curStr => utils.controlLength(curStr),
        },
        components: { topicAction },
        data() {
            return {
                count: 0,
                checkAll: false, // 全选
                topicTypeList: [{
					labelId: 1,
					labelText: '普通话题'
				}, {
					labelId: 2,
					labelText: '商品话题'
				}],
                dataList: [],
                loading: true,
                page: 1,
                showActionPage: false,
                topicInfo: {},
                filterData: {}
            }
        },
        mounted() {
           this.getData();
        },
        methods: {
            // 权益列表
            getMedalEquityList() {
                rightsList({
                    page: 0,
                    size: 50
                }).then(res => {
                    if (res.data.isSuccessful === "Y") {
                        this.medalEquityList  = res.data.data.rights;
                    }
                });
            },
            // 获取类型
            getType(labelId) {
                const info = this.topicTypeList.find(item => item.labelId === labelId) || {}
                return info.labelText || ''
            },
            // 搜索
            search() {
                this.getData();
            },
            // 加载分页数据
            handleCurrentChange(val) {
                this.page = val;
                this.getData();
            },
            // 全选
            handleCheckAllChange(val) {
                if (this.checkAll) {
                    this.$refs.multipleTable.toggleAllSelection();
                } else {
                    this.$refs.multipleTable.clearSelection();
                }
            },
            resetQuery() {
                this.filterData = {};
                this.page = 1;
                this.getData();
            },

            handleCloseAction() {
                this.topicInfo = {}
            },
            // 话题分类
            handleTypeChange(e) {
                this.filterData.topicType = e;
                this.getData()
            },
            handleActionPage() {
                this.showActionPage = true;
            },
            closeTemplatePage(data) {
                this.handleCloseAction()
                this.showActionPage = data.action;
                data.refresh && this.getData();
            },
            // 监听选择的内容
            handleSelectionChange(val) {
                this.checkAll = this.dataList.length === val.length
            },
            // 获取数据
            getData() {
                medalList({
                    ...this.filterData,
                    page: this.page - 1
                }).then(res => {
                    if (res.data.isSuccessful === "Y") {
                        const { count, medalList } = res.data.data;
                        this.count = count;
                        this.dataList  = medalList;
                    }
                })
                .catch(err => {})
                .finally(() => {
                    if (this.loading) {
                        this.loading = false
                    }
                });
            },
            // 编辑
            handleEdit(info) {
                this.topicInfo = info;
                this.handleActionPage();
                // 同步数据
				this.$nextTick(() => {
					this.$refs.topicRef.updateData()
				})
            },
            // 更改状态
            handleUpdate(info) {
                this.$confirm( `是否${info.medalStatus ? '启用' : '停用'}?`, "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(() => {
                     let medalStatus = info.medalStatus ? 0 : 1;
					updateMedalState({
                        topicUid: info.topicUid,
                        medalStatus
                    }).then(res => {
                        if(res.data.isSuccessful !== "Y"){
                            this.$message.error(res.data.message);
                            return
                        }

                        this.$message.success(res.data.message);
                        setTimeout(() => {
                            this.getData();
                        }, 500)
                    }).catch(err => {});
				}).catch();
            },
            // 删除
            handleDel(info) {
                this.$confirm("删除操作，是否继续?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(() => {
					deleteMedal({
                        topicUid: info.topicUid,
                    }).then(result => {
                        if(result.data.isSuccessful !== "Y"){
                            this.$message.error(result.data.message);
                            return
                        }

                        this.$message.success('成功删除！');
                        this.dataList = this.dataList.filter(item => item.topicUid  !== info.topicUid);
                        setTimeout(() => {
                            this.getData();
                        }, 500)
                    }).catch(err => {});
				}).catch();
            },
        }
    }
</script>
<style lang="scss" scoped>
    .rightItems {
        display: flex;
        align-content: center;
        margin-bottom: 10px;
        &_name {
            margin-left: 10px;
            text-align: left;
            font-size: 12px;
            line-height: 1.8;
        }
        &_icon {
            height: 40px;
            width: 40px;
            border-radius: 5px;
            object-fit: cover;
        }
    }
</style>
